﻿/* 
%include "/projects/hsieh_project/proj_201809/code_1_data/data_0_jobcr.sas" /source2;

Calculation of job creation and destruction at the establishment level.

Unit of analysis by definition:
1 3 5: flbdnum = firmnum + lbdid. We consider the same establishment with different owners as different establishments.
2 4 6: lbdid.

Definition of "exit" in the next stage of analysis ("entry" is defined accordingly)

1: 1. Exit; 2. Change in industry; 3. Change of ownership.
    - pv_byone = city ch_ind : We consider the same est in different ind as different est
2: 1. Exit; 2. Change in industry.
    - pv_byone = city ch_ind : We consider the same est in different ind as different est

3: 1. Exit; 2. Change of ownership.
    - pv_byone = city : We consider the same est in different ind as the same est
    - In addition, we sum by the est's industry in year1 (essentially we assume it does not change industry)
4: 1. Exit.
    - pv_byone = city : We consider the same est in different ind as the same est
    - In addition, we sum by the est's industry in year1 (essentially we assume it does not change industry)

5: 1. Exit; 2. Change of ownership. Change in industry does not count as incumbent.
    - pv_byone = city, pv_bytwo = ch_ind : We differentiate the est that changes industry by making use of pv_bytwo (it will be assigned status code 5 and 6)
6: 1. Exit. Change in industry does not count as incumbent.
    - pv_byone = city, pv_bytwo = ch_ind : We differentiate the est that changes industry by making use of pv_bytwo (it will be assigned status code 5 and 6)
*/

%include "/projects/hsieh_project/code_0_general/m_unit_status.sas" /source2;
%include "/projects/hsieh_project/code_0_general/m_sum_by_status.sas" /source2;
%include "/projects/hsieh_project/proj_201809/code_1_data/m_sum_by_stat_alt.sas";


/*
================================================================================
Macro that loops over selected years for a given definition

Aggregate employment, plants, and pay by status
Using the appropriate pv_byone, pv_bytwo, and aggregation programs
*/

%macro jc_agg(dt_in= , dt_out= , i_def= , c_city= , c_ind= , c_add= , c_id= , c_code= , l_year= );

%put Calculate Job Creation;
%put Input &dt_in.;
%put Output &dt_out.;
%put City &c_city.;
%put Industry &c_ind.;
%put Additional &c_add.;
%put Basic Unit &c_id.;
%put Code &c_code.;
%put Years &l_year.;

data dt_proc;
  set &dt_in.;
run;

%if &i_def.=1 | &i_def.=2 %then %do;
  %put Definition &i_def.: Include Change in Industry;
  %m_unit_status(dsi_in=dt_proc, dsi_out=dt_proc, pv_byone=%bquote(&c_city. &c_ind.), pv_bytwo= , pv_unit=&c_id., pv_status=&c_code._stat, pl_year=%bquote(&l_year.));
  %Let by_var=year &c_city. &c_ind. &c_add.;
  %m_sum_by_status(dsi_in=dt_proc, dsi_out=&dt_out., pv_by=%bquote(&by_var.), pv_status=&c_code._stat, pl_year=%bquote(&l_year.));
%end;

%if &i_def.=3 | &i_def.=4 %then %do;
  %put Definition &i_def.: Exclude Change in Industry;
  %m_unit_status(dsi_in=dt_proc, dsi_out=dt_proc, pv_byone=%bquote(&c_city.), pv_bytwo= , pv_unit=&c_id., pv_status=&c_code._stat, pl_year=%bquote(&l_year.));
  %Let by_var=year &c_city. &c_ind. &c_add.;
  %run_sum_by_stat_alt(ds=dt_proc, ds_out=&dt_out., by_var=%bquote(&by_var.), c_unit=&c_code., c_id=&c_id., ll_year=%bquote(&l_year.)); /* This is still the old version */
%end;

%if &i_def.=5 | &i_def.=6 %then %do;
  %put Definition &i_def.: Separate Change in Industry from Incumbent;
  %m_unit_status(dsi_in=dt_proc, dsi_out=dt_proc, pv_byone=%bquote(&c_city.), pv_bytwo=%bquote(&c_ind.), pv_unit=&c_id., pv_status=&c_code._stat, pl_year=%bquote(&l_year.));
  %Let by_var=year &c_city. &c_ind. &c_add.;
  %m_sum_by_status(dsi_in=dt_proc, dsi_out=&dt_out., pv_by=%bquote(&by_var.), pv_status=&c_code._stat, pl_year=%bquote(&l_year.));
%end;

%mend;


/*
================================================================================
For a given definition, calls jc_agg and rename variables

Recall definition:
Definition 1 for Exit: 1. Exit; 2. Change in industry; 3. Change of ownership.
Definition 2 for Exit: 1. Exit; 2. Change in industry.
Definition 3 for Exit: 1. Exit; 2. Change of ownership.
Definition 4 for Exit: 1. Exit.
Definition 5 for Exit: 1. Exit; 2. Change of ownership. Change in industry does not count as incumbent.
Definition 6 for Exit: 1. Exit. Change in industry does not count as incumbent.
*/

/* Clean for definition i */

%macro jc_def(dt_in= , dt_out= , i_def= , c_city= , c_ind= , c_add= , c_code= , l_year= );

%if &i_def.=1 | &i_def.=3 | &i_def.=5 %then %do;
  %Let c_id=flbdnum;
%end;
%if &i_def.=2 | &i_def.=4 | &i_def.=6 %then %do;
  %Let c_id=lbdid;
%end;

%jc_agg(dt_in=&dt_in., dt_out=&dt_out._&i_def., i_def=&i_def., c_city=&c_city., c_ind=&c_ind., c_add=%bquote(&c_add.), c_id=&c_id., c_code=&c_code., l_year=%bquote(&l_year.));

data &dt_out._&i_def.;
  set &dt_out._&i_def.;
  rename emp_stat=emp_s&i_def. est_stat=est_s&i_def. pay_stat=pay_s&i_def.;
  rename year_stat1=syear1 year_stat2=syear2;
run;

data &dt_out._&i_def.;
  set &dt_out._&i_def.;
  keep year syear1 syear2 &c_city. &c_ind. &c_add. &c_code._stat emp_s&i_def. est_s&i_def. pay_s&i_def.;
run;

proc sort data=&dt_out._&i_def.; by year syear1 syear2 &c_city. &c_ind. &c_add. &c_code._stat;

%mend;

/*
================================================================================
Macro that 
1) loops over selected definitions
2) merges the resulting data sets for each definition together
*/

%macro jc_main(dt_in= , dt_out= , c_city= , c_ind= , c_add= , c_code= , l_year= , l_def= );

/* Loop over selected definitions */
%Let ic_def = 1;
%do %while (%scan(%bquote(&l_def.), &ic_def.) ~= );
  %Let i_def=%scan(%bquote(&l_def.), &ic_def.);
  %put Main Loop Definition &i_def.;
  
  %jc_def(dt_in=&dt_in., dt_out=&dt_out., i_def=&i_def., c_city=&c_city., c_ind=&c_ind., c_add=%bquote(&c_add.), c_code=&c_code., l_year=%bquote(&l_year.));
  
  %if &ic_def.=1 %then %do;
    data &dt_out.;
      set &dt_out._&i_def.;
    run;
  %end;
  %else %do;
    data &dt_out.;
      merge &dt_out. &dt_out._&i_def.;
      by year syear1 syear2 &c_city. &c_ind. &c_add. &c_code._stat;
    run;
  %end;
  
  %Let ic_def = %eval(&ic_def. + 1);
%end;

%mend;

/* End of SAS file */
